3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
14 * Table name prefixes. Oracle likes it shorter.
16 const DB_PREFIX
= 'unittest_';
17 const ORA_DB_PREFIX
= 'ut_';
19 protected $supportedDBs = array(
25 function __construct( $name = null, array $data = array(), $dataName = '' ) {
26 parent
::__construct( $name, $data, $dataName );
28 $this->backupGlobals
= false;
29 $this->backupStaticAttributes
= false;
32 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
33 /* Some functions require some kind of caching, and will end up using the db,
34 * which we can't allow, as that would open a new connection for mysql.
35 * Replace with a HashBag. They would not be going to persist anyway.
37 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
39 if( $this->needsDB() ) {
43 $this->db
= wfGetDB( DB_MASTER
);
45 $this->checkDbIsSupported();
47 $this->oldTablePrefix
= $wgDBprefix;
52 $this->addCoreDBData();
55 parent
::run( $result );
60 parent
::run( $result );
66 function __destruct() {
67 if( $this->needsDB() ) {
73 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
77 $rc = new ReflectionClass( $this );
78 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
82 * Stub. If a test needs to add additional data to the database, it should
83 * implement this method and do so
85 function addDBData() {}
87 private function addCoreDBData() {
89 User
::resetIdByNameCache();
92 $user = User
::newFromName( 'UTSysop' );
94 if ( $user->idForName() == 0 ) {
95 $user->addToDatabase();
96 $user->setPassword( 'UTSysopPassword' );
98 $user->addGroup( 'sysop' );
99 $user->addGroup( 'bureaucrat' );
100 $user->saveSettings();
104 //Make 1 page with 1 revision
105 $article = new Article( Title
::newFromText( 'UTPage' ) );
106 $article->doEdit( 'UTContent',
110 User
::newFromName( 'UTSysop' ) );
113 private function initDB() {
116 $dbType = $this->db
->getType();
118 if ( $wgDBprefix === $this->dbPrefix() ) {
119 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
122 $tables = $this->listTables();
124 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $this->dbPrefix() );
125 $this->dbClone
->useTemporaryTables( $this->useTemporaryTables
);
126 $this->dbClone
->cloneTableStructure();
128 if ( $dbType == 'oracle' )
129 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
131 if ( $dbType == 'oracle' ) {
132 # Insert 0 user to prevent FK violations
135 $this->db
->insert( 'user', array(
137 'user_name' => 'Anonymous' ) );
142 protected function destroyDB() {
144 if ( $this->useTemporaryTables
) {
145 # Don't need to do anything
147 //Temporary tables seem to be broken ATM, delete anyway
150 if( is_null( $this->db
) ) {
154 $tables = $this->db
->listTables( $this->dbPrefix(), __METHOD__
);
156 foreach ( $tables as $table ) {
158 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
159 $this->db
->query( $sql, __METHOD__
);
160 } catch( Exception
$e ) {
164 if ( $this->db
->getType() == 'oracle' )
165 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__
);
167 CloneDatabase
::changePrefix( $this->oldTablePrefix
);
170 function __call( $func, $args ) {
171 static $compatibility = array(
172 'assertInternalType' => 'assertType',
173 'assertNotInternalType' => 'assertNotType',
174 'assertInstanceOf' => 'assertType',
175 'assertEmpty' => 'assertEmpty2',
178 if ( method_exists( $this->suite
, $func ) ) {
179 return call_user_func_array( array( $this->suite
, $func ), $args);
180 } elseif ( isset( $compatibility[$func] ) ) {
181 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
183 throw new MWException( "Called non-existant $func method on "
184 . get_class( $this ) );
188 private function assertEmpty2( $value, $msg ) {
189 return $this->assertTrue( $value == '', $msg );
192 static private function unprefixTable( $tableName ) {
194 return substr( $tableName, strlen( $wgDBprefix ) );
197 protected function listTables() {
200 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
201 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
203 if ( $this->db
->getType() == 'sqlite' ) {
204 $tables = array_flip( $tables );
205 // these are subtables of searchindex and don't need to be duped/dropped separately
206 unset( $tables['searchindex_content'] );
207 unset( $tables['searchindex_segdir'] );
208 unset( $tables['searchindex_segments'] );
209 $tables = array_flip( $tables );
215 protected function checkDbIsSupported() {
216 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
217 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
221 public function getCliArg( $offset ) {
223 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
224 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
229 public function setCliArg( $offset, $value ) {
231 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;